import {
    deleteById,
    searchPage,
    searchColors,
    searchGiftPage,
    searchBrands,
    searchTypes,
    saveGift,
    updateGift
} from '../../api/gift'
import {
    searchGiftGoodCheck,
    deleteGiftGoodById,
    searchGiftGoodPage,
    searchGiftGoodColors,
    searchGiftGoodBrands,
    searchGiftGoodTypes,
    saveGiftGood,
    updateGiftGood
} from '../../api/gift'
import pickerOptions from '@/utils/date';
import {getBase64Str} from '../../utils/base64Utils'
import base64 from '../../utils/getbase64Str2';


let giftToGood = {
    data() {
        return {
            //日期组件
            pickerOptions: pickerOptions,
            //table的list
            tableData: [],
            giftTableData: [],
            total: 0,
            totalGift: 0, //入库量+赠品量的总和
            searchParams: {
                currentPage: 1,
                pageSize: 10
            },
            searchGiftParams: {
                currentPage: 1,
                pageSize: 5
            },
            colorList: [],
            brandList: [],
            typeList: [],
            giftCheckList: [],
            innerVisible: false,
            dialogVisibleChooseGift: false,
            formDialogVisible: false,
            editDialogVisible: false,
            checkDialogVisible: false,
            formData: {},
            editForm: {},
            imageUrl: ''
        };
    },
    methods: {
        // indexMethod(index) {
        //     let curpage = this.currentPage; //单前页码，具体看组件取值
        //     let limitpage = this.PageSize; //每页条数，具体是组件取值
        //     return index + 1 + (curpage - 1) * limitpage;
        // },

        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange() {

        },
        currentPageChange(currentPage) {
            //console.log(currentPage);
            this.searchParams.currentPage = currentPage;
            this.findPage();
        },
        currentGiftPageChange(currentPage) {
            //console.log(currentPage);
            this.searchGiftParams.currentPage = currentPage;
            this.findGiftPage();
        },
        /*  findPage(){
              searchPage(this.searchParams).then(response=>{
                 this.total= response.total;
                 this.tableData = response.data;
              })
          },  */
        //第二种写法：
        async findPage() {
            let response = await searchGiftGoodPage(this.searchParams);
            this.total = response.total;
            console.log(response.data);
            this.tableData = response.data;
        },
        async findGiftPage() {
            let response = await searchGiftPage(this.searchGiftParams);
            this.total = response.total;
            console.log(response.data);
            this.giftTableData = response.data;
        },
        chooseTime() {
            this.searchParams.startTime = this.pickerOptions.startArrayDate[0];
            this.searchParams.endTime = this.pickerOptions.startArrayDate[1];
        },
        resetForm() {
            this.searchParams = {currentPage: 1, pageSize: 10};
            this.pickerOptions.startArrayDate = '';
            this.findPage();

        },
        //async表示异步调用 await：表示执行完这条语句，才执行下面语句
        async deleteById() {
            console.log(this.formData.giftGoodid);
            await deleteGiftGoodById(this.formData.giftGoodid);
            this.findPage();
        },
        // getImgStr(e){
        //     console.log("imgstr===");
        //     console.log(e.file);
        //     let fileReader = new FileReader();
        //     fileReader.readAsDataURL(e.file);
        //     fileReader.onload= e=> {
        //         console.log("onload===");
        //         console.log(e);
        //         console.log("图片的base64 e.target.result====");
        //         console.log(e.target.result);
        //         this.formData.brandLogo=e.target.result;
        //         this.imageUrl = e.target.result;
        //
        //     }
        //
        // },
        // getImgStr(e){
        //   getBase64Str(e.file).then(response=>{
        //       this.formData.brandLogo = response;
        //       this.imageUrl =  response;
        //   })
        //  },
        async init() {
            let response1 = await searchGiftGoodColors();
            let response2 = await searchGiftGoodBrands();
            let response3 = await searchGiftGoodTypes();
            let response4 = await searchGiftGoodCheck();
            this.colorList = response1;
            this.brandList = response2;
            this.typeList = response3;
            this.giftCheckList = response4;
        },
        getImgStr(e) {
            base64.getbase64Str(e.file).then(response => {
                this.formData.brandLogo = response;
                this.imageUrl = response;
            })

        },
        async addGiftGood() {
            this.formData.giftSum = this.formData.giftSum - this.formData.giftNum;
            await updateGift(this.formData);
            console.log(this.formData)
            await saveGiftGood(this.formData);
            this.findPage();
        },

        async getThisGift(row) {
            this.editForm = JSON.parse(JSON.stringify(row));
            console.log(this.editForm);
            this.totalGift = this.editForm.giftSum + this.editForm.giftNum;
        },
        async editGiftGood() {
            this.editForm.giftSum = this.totalGift - this.editForm.giftNum;
            await updateGift(this.editForm);
            await updateGiftGood(this.editForm);
            this.findPage();
        },
        async chooseThisGift(row) {
            this.formData = JSON.parse(JSON.stringify(row));
            console.log(this.editForm);
        },
        async giftData() {
            let response = await searchGiftPage(this.searchGiftParams);
            this.total = response.total;
            console.log(response.total);
            this.giftTableData = response.data;
        },
        parseDateFull(time) {
            var x = new Date(time);
            var z = {
                y: x.getFullYear(),
                M: x.getMonth() + 1,
                d: x.getDate(),
                h: x.getHours(),
                m: x.getMinutes(),
                s: x.getSeconds(),
            };
            if (z.M < 10) {
                z.M = "0" + z.M;
            }
            if (z.d < 10) {
                z.d = "0" + z.d;
            }
            if (z.h < 10) {
                z.h = "0" + z.h;
            }
            if (z.m < 10) {
                z.m = "0" + z.m;
            }
            return z.y + "年" + z.M + "月" + z.d + "日"+ z.h+"时" +z.h+"分";
        }

    },
    created() {
        this.findPage();
        this.init();
        this.giftData();
    }
};

export default giftToGood;
